package com.ry.java8;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

//Java8  stream测试
public class TestStream {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);

        TestStream testStream = new TestStream();

        Long cc =list.stream().filter(i -> i >2).count();
//        System.out.println(cc);

        testStream.test05();
    }



    //collect 由stream里面的值生成一个列表
    public void test01(){
        Stream stream = Stream.of("1","2","3");
        stream.collect(Collectors.toList());
    }

    //map  将一个流中的值转换成一个新的流
    public void test02(){
        List<String> list = Stream.of("a","b","c").map(string -> string.toLowerCase()).collect(Collectors.toList());
    }

    //filter 遍历数据并检查其中的元素时，可尝试使用Stream中提供的新方法filter
    public void test03(){
        Stream.of(1,2,3).filter( i -> i >1);
    }

    //flatMap 可以用Stream替换值，然后将多个Stream连接成一个Stream
    public void test04(){
        List list1 = new ArrayList();
        List list2 = new ArrayList();

        list1.add(1);
        list1.add(2);
        list2.add(3);
        list2.add(4);
        Stream.of(list1,list2).flatMap(number -> number.stream());
    }


    //max 和 min
    public void test05(){
        String s = Stream.of("222","3333","44444","1111").min(Comparator.comparing(string -> string.length())).get();
        System.out.println(s);
    }

//    public boolean moreThan(int num){
//        if(num > 1)
//            return true;
//          return false;
//    }

}
